/* eslint-disable func-names */
/* eslint-disable no-extend-native */
// declare global {
	interface String {
		replaceAll: (searchValue: string, replaceValue: string) => string;
		replaceAll2: (searchValue: string, replaceValue: string) => string;
		replaceRaw: (searchValue: string, replaceValue: string) => string;
		replaceRawAll: (searchValue: string, replaceValue: string) => string;
		setChar: (n: number, c: string) => string;
		// like "{1},{2}",["hello","world"]
		formats: (args: string[]) => string;
		// like "{1},{2}","hello,world"
		format: (args: string) => string;
	}
// }

function extendString(): void {
	// console.log("extend string");
	String.prototype.setChar = function (n: number, c: string): string {
		if (this.length < n) {
			return this + ' '.repeat(n - this.length) + c;
		}
		return this.substr(0, n) + c + this.substr(n + 1, this.length - 1 - n);
	};

	String.prototype.replaceAll = function (search: string, replacement: string): string {
		return this.replace(new RegExp(search, 'g'), replacement);
	};

	String.prototype.replaceAll2 = function (search: string, replacement: string): string {
		return this.split(search).join(replacement);
	};

	String.prototype.replaceRaw = function (src: string, des: string): string {
		const index = this.indexOf(src);
		if (index >= 0) {
			// console.log("replace_string",line,src,des)
			return this.substr(0, index) + des + this.substr(index + src.length);
		}
		return this.toString();
	};
	String.prototype.replaceRawAll = function (src: string, des: string): string {
		// eslint-disable-next-line @typescript-eslint/no-this-alias
		let s = this;
		let index = s.indexOf(src);
		while (index >= 0) {
			// console.log("replace_string",line,src,des)
			s = s.substr(0, index) + des + s.substr(index + src.length);
			index = s.indexOf(src);
		}
		return s.toString();
	};

	String.prototype.formats = function (args: string[]): string {
		// eslint-disable-next-line @typescript-eslint/no-this-alias
		if (!args) {
			return this.toString();
		}
		let msg = this;
		for (let k = 0; k < args.length; k++) {
			msg = msg.replace(`{${k}}`, args[k]);
		}
		return msg.toString();
	};
	String.prototype.format = function (arg: string): string {
		if (!arg) {
			return this.toString();
		}
		let args: string[];
		if (typeof arg !== 'string') {
			args = [String(arg)];
		} else {
			args = arg.split(',');
		}
		return this.formats(args);
	};

	// console.log(String)
}
